﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Model;

namespace DAL
{
    public class D_userPlan
    {
    public D_userPlan()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
    }
    #region 方法

        /// <summary>
        /// 新增计划表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
    public int Add(M_userPlan model)
    { 
        string str = "P_AddUserPlan";
        SqlParameter[] sqlParameters = {
            new SqlParameter("@PlanSubject", SqlDbType.VarChar,500),
            new SqlParameter("@PlanBody", SqlDbType.VarChar,5000),
            new SqlParameter("@Category", SqlDbType.VarChar,50),
            new SqlParameter("@Progress", SqlDbType.VarChar,50),
            new SqlParameter("@StartTime", SqlDbType.DateTime),
            new SqlParameter("@EndTime", SqlDbType.DateTime),
            new SqlParameter("@Creater", SqlDbType.VarChar,50),
            new SqlParameter("@Helper", SqlDbType.VarChar,50),
        };
        sqlParameters[0].Value = model.PlanSubject;
        sqlParameters[1].Value = model.PlanBody;
        sqlParameters[2].Value = model.Category;
        sqlParameters[3].Value = model.Progress;
        sqlParameters[4].Value = model.StartTime;
        sqlParameters[5].Value = model.EndTime;
        sqlParameters[6].Value = model.Creater;
        sqlParameters[7].Value = model.Helper;

        if (DbHelper.ExecuteCommand(str, sqlParameters) > 0)
        {
            return 1;
        }
        else
        {
            return 0;
        }

    }

        /// <summary>
        /// 更新计划表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
    public int Update(M_userPlan model)
    {
        string str = "P_UpdateUserPlan";
        SqlParameter[] sqlParameters = {
            new SqlParameter("@Id", SqlDbType.Int,4),                               
            new SqlParameter("@PlanSubject", SqlDbType.VarChar,500),
            new SqlParameter("@PlanBody", SqlDbType.VarChar,5000),
            new SqlParameter("@Category", SqlDbType.VarChar,50),
            new SqlParameter("@Progress", SqlDbType.VarChar,50),
            new SqlParameter("@StartTime", SqlDbType.DateTime),
            new SqlParameter("@EndTime", SqlDbType.DateTime),
            new SqlParameter("@Helper", SqlDbType.VarChar,50),
        };
        sqlParameters[0].Value = model.ID;
        sqlParameters[1].Value = model.PlanSubject;
        sqlParameters[2].Value = model.PlanBody;
        sqlParameters[3].Value = model.Category;
        sqlParameters[4].Value = model.Progress;
        sqlParameters[5].Value = model.StartTime;
        sqlParameters[6].Value = model.EndTime;
        sqlParameters[7].Value = model.Helper;

        if (DbHelper.ExecuteCommand(str, sqlParameters) > 0)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

    public int Delete(int id)
    {
        string str = "P_DeleteUserPlan";
        SqlParameter[] sqlParameters = {                             
            new SqlParameter("@Id", SqlDbType.BigInt)
        };
        sqlParameters[0].Value = id;

        if (DbHelper.ExecuteCommand(str, sqlParameters) > 0)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    public int UpdateM(string strManagerRemark, string strUserRemark, string strUser, DateTime dt)
    {
        string str = "P_UpdateUserPlanM";
        SqlParameter[] sqlParameters = {
            new SqlParameter("@creater", SqlDbType.VarChar,50),                               
            new SqlParameter("@createtime", SqlDbType.DateTime),
            new SqlParameter("@ManagerRemark", SqlDbType.VarChar,5000),
            new SqlParameter("@UserRemark", SqlDbType.VarChar,5000)
        };
        sqlParameters[0].Value = strUser;
        sqlParameters[1].Value = dt;
        sqlParameters[2].Value = strManagerRemark;
        sqlParameters[3].Value = strUserRemark;
        if (DbHelper.ExecuteCommand(str, sqlParameters) > 0)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

    private M_userPlan Reader(SqlDataReader reader)
    {
        M_userPlan model = new M_userPlan();
        object obj;
        obj = reader["Id"];
        if ((obj != null) && (obj != DBNull.Value))
        {
            model.ID = Convert.ToInt32(obj);
        }
        model.PlanSubject = reader["PlanSubject"].ToString();
        model.PlanBody = reader["PlanBody"].ToString();
        model.Category = reader["Category"].ToString();
        model.Progress = reader["Progress"].ToString();
        obj = reader["StartTime"];
        if ((obj != null) && (obj != DBNull.Value))
        {
            model.StartTime = (DateTime)obj;
        }
        obj = reader["EndTime"];
        if ((obj != null) && (obj != DBNull.Value))
        {
            model.EndTime = (DateTime)obj;
        }
        model.Creater = reader["Creater"].ToString();
        obj = reader["CreateTime"];
        if ((obj != null) && (obj != DBNull.Value))
        {
            model.CreateTime = (DateTime)obj;
        }
        model.Helper = reader["Helper"].ToString();
        return model;
    }

    private M_userPlan Reader(DataTable dt)
    {
        M_userPlan model = new M_userPlan();
        if (dt.Rows.Count > 0)
        {
            if (dt.Rows[0]["Id"].ToString() != "")
            {
                model.ID = int.Parse(dt.Rows[0]["Id"].ToString());
            }
            model.PlanSubject = dt.Rows[0]["PlanSubject"].ToString();
            model.PlanBody = dt.Rows[0]["PlanBody"].ToString();
            model.Category = dt.Rows[0]["Category"].ToString();
            model.Progress = dt.Rows[0]["Progress"].ToString();
            model.Creater = dt.Rows[0]["Creater"].ToString();
            if (dt.Rows[0]["StartTime"].ToString() != "")
            {
                model.StartTime = DateTime.Parse(dt.Rows[0]["StartTime"].ToString());
            }
            if (dt.Rows[0]["EndTime"].ToString() != "")
            {
                model.EndTime = DateTime.Parse(dt.Rows[0]["EndTime"].ToString());
            }            
            return model;
        }
        else
        {
            return null;
        }
    }

    public IList<M_userPlan> GetIList(string strCreater, DateTime dt)
    {
        string str = "P_SelectUserPlan";
        SqlParameter[] sqlParameters ={
        new SqlParameter("@Creater",SqlDbType.VarChar,50),
        new SqlParameter("@CreateTime",SqlDbType.DateTime)
                                   };
        sqlParameters[0].Value = strCreater;
        sqlParameters[1].Value = dt;

        SqlDataReader sqlDtReader = DbHelper.getReader(str, sqlParameters);
        IList<M_userPlan> ilst = new List<M_userPlan>();
        while (sqlDtReader.Read())
        {
            ilst.Add(Reader(sqlDtReader));
        }
        return ilst;
    }

        
    public IList<M_userPlan> GetListForManager(string strCreater,DateTime dt)
    {
        string str = "P_SelectUserPlanforManager";
        SqlParameter[] sqlParameters ={
        new SqlParameter("@Creater",SqlDbType.VarChar,50),
        new SqlParameter("@CreateTime",SqlDbType.DateTime)
                                   };
        sqlParameters[0].Value = strCreater;
        sqlParameters[1].Value = dt;
        SqlDataReader sqlDtReader = DbHelper.getReader(str, sqlParameters);
        IList<M_userPlan> ilst = new List<M_userPlan>();
        while (sqlDtReader.Read())
        {
            ilst.Add(Reader(sqlDtReader));
        }
        return ilst;
    }

    public IList<M_userPlan> GetIList(string strCreater)
    {
        string str = "P_SelectUserTodayPlan";
        SqlParameter[] sqlParameters ={
        new SqlParameter("@Creater",SqlDbType.VarChar,50)
                                   };
        sqlParameters[0].Value = strCreater;
        SqlDataReader sqlDtReader = DbHelper.getReader(str, sqlParameters);
        IList<M_userPlan> ilst = new List<M_userPlan>();
        while (sqlDtReader.Read())
        {
            ilst.Add(Reader(sqlDtReader));
        }
        return ilst;
    }        

    public IList<M_userPlan> GetIListForManager(string strCreater)
    {
        string str = "P_SelectUserTodayPlanForManager";
        SqlParameter[] sqlParameters ={
        new SqlParameter("@Creater",SqlDbType.VarChar,50)
                                   };
        sqlParameters[0].Value = strCreater;
        SqlDataReader sqlDtReader = DbHelper.getReader(str, sqlParameters);
        IList<M_userPlan> ilst = new List<M_userPlan>();
        while (sqlDtReader.Read())
        {
            ilst.Add(Reader(sqlDtReader));
        }
        return ilst;
    }

    public M_userPlan GetModel(int Id)
    { 
        string str = "P_SelectUserPlanById";
        SqlParameter[] sqlParameters ={
        new SqlParameter("@Id", SqlDbType.Int,4)                                   
                                      };
        sqlParameters[0].Value = Id;
        DataTable sqlDt = DbHelper.getDateSet(str, sqlParameters);

        return Reader(sqlDt);
    }

    public IList<string> GetRemark(string strCreater, DateTime dt)
    {
        string str = "P_SelectRemark";
        SqlParameter[] sqlParameters ={
        new SqlParameter("@Creater",SqlDbType.VarChar,50),
        new SqlParameter("@CreateTime",SqlDbType.DateTime)
                                   };
        sqlParameters[0].Value = strCreater;
        sqlParameters[1].Value = dt;
        DataTable sqlDt = DbHelper.getDateSet(str, sqlParameters);
        IList<string> ilst = new List<string>();
        if (sqlDt.Rows.Count > 0)
        {
            ilst.Add(sqlDt.Rows[0]["ManagerRemark"].ToString());
            ilst.Add(sqlDt.Rows[0]["UserRemark"].ToString());
        }
        
        return ilst;
    }

    #endregion 方法
    }
}
